﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;

public partial class ueditor_dialogs_image_up : System.Web.UI.Page
{
    String uploadPath = "Image/";   //保存路径
    String fileType = ".jpg,.jpeg,.gif,.png,.bmp";   //文件允许格式
    Int32 fileSize = 3*1024;    //文件大小限制，单位KB
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            HttpPostedFile oFile = Request.Files[0];
            string fileExtension = System.IO.Path.GetExtension(oFile.FileName).ToLower();
            if (fileType.ToLower().IndexOf(fileExtension) > -1)//检测是否为允许的上传文件类型
            {
                if (this.fileSize * 1024 >= oFile.ContentLength)
                {
                    try
                    {
                        string DirectoryPath;
                        DirectoryPath = uploadPath + DateTime.Now.ToString("yyyy-MM");
                        string sFileName = DateTime.Now.ToString("yyyyMMddHHmmssffff");  //文件名称
                        string FullPath = "~/" + DirectoryPath + "/" + sFileName + fileExtension;//最终文件路径
                        if (!Directory.Exists(Server.MapPath("~/" + DirectoryPath)))
                            Directory.CreateDirectory(Server.MapPath("~/" + DirectoryPath));
                        oFile.SaveAs(Server.MapPath(FullPath));
                        Response.Write("<script type='text/javascript'>parent.reloadImg('" + Page.ResolveUrl(FullPath) + "');" + "location.href='upload.aspx?url=" + Page.ResolveUrl(FullPath) + "';</script>");

                    }
                    catch (Exception ex)
                    {
                        //WebHelper.AlertAndRedirect("上传文件失败。" + ex.Message, "upload.aspx");
                        ClientScript.RegisterClientScriptBlock(this.GetType(), "upload.aspx", "alert('" + ex.Message + ",文件上传失败！')", true);
                    }
                }
                else
                {
                    //WebHelper.AlertAndRedirect("上传文件大小超过限制。", "upload.aspx");
                    ClientScript.RegisterClientScriptBlock(this.GetType(), "upload.aspx", "alert('上传文件大小超过限制')", true);
                }
            }
            else
            {
                //WebHelper.AlertAndRedirect("上传文件扩展名是不允许的扩展名。", "upload.aspx");
                ClientScript.RegisterClientScriptBlock(this.GetType(), "upload.aspx", "alert('上传文件扩展名是不允许的扩展名！')", true);
            }
        }
    }
}
